package com.crlui.manager
{
	import com.crlui.core.IRenderable;
	import com.crlui.core.IResizeable;
	
	import flash.display.Stage;
	import flash.events.Event;

	public class ResizeManager
	{
		protected var currentW:int;
		protected var currentH:int;
		
		private var _stage:Stage;
		private var list:Vector.<IResizeable>;
		public static var instance:ResizeManager=new ResizeManager();
		public function ResizeManager()
		{
			list=new Vector.<IResizeable>();
		}
		public function set stage(value:Stage):void{
			_stage=value;
			
			currentW=_stage.stageWidth;
			currentH=_stage.stageHeight;
			
			_stage.addEventListener(Event.RESIZE,resizeHandle);
		}
		
		public function add(value:IResizeable):Boolean{
			if(list.indexOf(value) !=-1){
				return false;
			}
			
			update(value);
			
			list.push(value);
			return true;
		}
		
		public function remove(value:IResizeable):Boolean{
			var index:int=list.indexOf(value);
			if(index ==-1){
				return false;
			}
			
			list.splice(index,1);
			return true;
		}
		
		public function update(value:IResizeable):void{
			value.resize(currentW,currentH);
		}
		
		public function resizeHandle(event:Event):void{
			var w:int=_stage.stageWidth;
			var h:int=_stage.stageHeight;
			
			if(currentH==w && currentW==h){
				return;
			}
			
			currentW=w;
			currentH=h;
			
			for each(var item:IResizeable in list){
				item.resize(currentW,currentH);
			}
		}
	}
}